import { entryCheckUrl, userStatusUrl, entryIdCardOCRUrl } from '../../../../api/individual'
import { post, uploadFile, formatDate, initUploadFileListKey, individualEntry } from '../../../../utils/index'
import WxValidate from '../../../../utils/WxValidate'

Page({
  data: {
    form: {
      name: '',
      phone: '',
      idCard: '',
      // name: '林金鼎',
      // phone: '',
      // idCard: '350625199312101515',
      // address: '福建省长泰县枋洋镇林溪村大路口39号',
      // birthday: '1993/12/10',
      // gender: '男',
      // nation: '汉',
      // idCardImgFront: '/xmhr/upload/20200720/c893f5ee2d2e32b25b05ba54fa3a1752.jpg',
      // idCardImgFrontId: 1285121391074521089,
      // idCardImgBack: '/xmhr/upload/20200720/0f49c2d3a3bcb7a0391ae7d42e61a791.jpg',
      // idCardImgBackId: 1285121906478985217,
      isLongValid: false,
      idCardExpiryDate: ''
    },
    datePopup: false,
    idCardFontList: [],
    idCardBackList: [],
    formError: {},
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    maxDate: new Date((new Date().getFullYear() + 20).toString()).getTime(),
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return `${value}日`
    }
  },
  async verified () { // 验证
    const { form } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    this.setData({ formError: {} })
    await post(entryCheckUrl, form)
    const { data } = await post(userStatusUrl)
    individualEntry(data)
  },
  initValidator () { // 初始化验证器
    const rules = {
      name: { required: true, maxlength: 16 },
      phone: { required: true, tel: true },
      idCard: { required: true, idcard: true },
      idCardImgFrontId: { required: true },
      idCardImgBackId: { required: true },
    }
    const messages = {
      name: { required: '请输入姓名', maxlength: '姓名长度不大于16位' },
      phone: { required: '请输入手机号', tel: '请填写正确手机号' },
      idCard: { required: '请输入身份证号', idcard: '请填写正确身份证号' },
      idCardImgFrontId: { required: '请上传身份证正面人像照' },
      idCardImgBackId: { required: '请上传身份证反面国徽照' }
    }
    if (!this.data.form.isLongValid) {
      rules.idCardExpiryDate = { required: true }
      messages.idCardExpiryDate = { required: '请选择身份证有效日期' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  nameChange ({ detail }) { // 姓名
    this.data.form.name = detail
  },
  phoneChange ({ detail }) { // 手机号
    this.data.form.phone = detail
  },
  idCardChange ({ detail }) { // 身份证
    this.data.form.idCard = detail
  },
  genderChange ({ detail }) { // 性别
    this.data.form.gender = detail
  },
  nationChange ({ detail }) { // 民族
    this.data.form.nation = detail
  },
  birthdayChange ({ detail }) { // 出生年月
    this.data.form.birthday = detail
  },
  birthdayChange ({ detail }) { // 身份证住址
    this.data.form.birthday = detail
  },
  longValidChange ({ detail }) { // 身份证是否长期有效
    const idCardExpiryDate = detail ? '长期有效' : ''
    this.setData({ 'form.isLongValid': detail, 'form.idCardExpiryDate': idCardExpiryDate })
    this.initValidator()
  },
  dateClick () { // 弹窗
    this.setData({ datePopup: true })
  },
  dateChange ({ detail }) { // 选择日期
    const idCardExpiryDate = formatDate(new Date(detail), 'yyyy-MM-dd')
    this.setData({ 'form.idCardExpiryDate': idCardExpiryDate })
    this.dateClose()
  },
  dateClose () { // 关闭时间选择
    this.setData({ datePopup: false })
  },
  async idCardFontRead ({ detail }) { // 身份正面上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ idCardFontList: [data] })
    this.data.form.idCardImgFront = data.link
    this.data.form.idCardImgFrontId = data.id
    this.idCardOCR()
  },
  idCardFontDel () { // 删除身份正面
    this.data.form.idCardImgFront = ''
    this.data.form.idCardImgFrontId = ''
    this.setData({
      idCardFontList: []
    })
  },
  async idCardBackRead ({ detail }) { // 身份反面上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ idCardBackList: [data] })
    this.data.form.idCardImgBack = data.link
    this.data.form.idCardImgBackId = data.id
  },
  idCardBackDel () { // 删除身份反面
    this.data.form.idCardImgBack = ''
    this.data.form.idCardImgBackId = ''
    this.setData({
      idCardBackList: []
    })
  },
  async idCardOCR () { // 身份证ocr
    const { form } = this.data
    const { data } = await post(entryIdCardOCRUrl, { path: form.idCardImgFront }, {}, false)
    this.setData({
      'form.name': data.name,
      'form.idCard': data.idNo,
      'form.address': data.address,
      'form.birthday': data.birthDay,
      'form.gender': data.gender,
      'form.nation': data.nation
    })
  },
  initPhone () { // 自动获取手机号
    const user = wx.getStorageSync('user')
    this.setData({ 'form.phone': user.user_name })
  },
  onLoad () {
    this.initValidator()
    this.initPhone()
  }
})